/*
*******************************************************************************
* ansi c source code
* file name:
*	Taps_Model.c
* abstract:
*	This is a model based on 5 Taps with 0,8,16,24,32 samples delay each.
*	It is a time-invariance model and the power distribution is fixed.
* author:Fangming He
*******************************************************************************
*/



/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <string.h>
#include "../common/globaldef.h"
#include "../common/globalVai.h"

/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
*	This is a model based on 5 Taps with 0,8,16,24,32 samples delay each.
*	It is a time-invariance model and the power distribution is fixed.
*  input :
*  *ISI  : the last OFDM symbol interferce
*  *Symbol: the address of OFDM symbol
*  output :
*  none
*	Cao Yanyan	2006-05-27
* revision history:
*	Cao Yanyan	2006-05-27	original version
*******************************************************************************
*/

void Taps_Model(struct Complex *ISI,struct Complex  *Symbol)
{
	double Taps_power[5]={0.63640864655883,0.23412165725274,0.08612854443627,0.03168492079612,0.01165623095604};
	int i,j,Length;
	struct Complex  ISI_temp[MaxDelay];
	struct Complex  tmp;
	Length=CP_Length+FFT_Size;
	memset(ISI_temp,0,MaxDelay * sizeof (struct Complex));
   	/*calculate the ISI between samples because of the delay*/
	for(i=0;i<8;i++)
	{
		for(j=1;j<5;j++)
		{
			ISI_temp[i].real+=Symbol[Length-j*8+i].real*Taps_power[j];
			ISI_temp[i].imag+=Symbol[Length-j*8+i].imag*Taps_power[j];
		}
	}

	for(i=8;i<16;i++)
	{
		for(j=2;j<5;j++)
		{
			ISI_temp[i].real+=Symbol[Length-j*8+i].real*Taps_power[j];
			ISI_temp[i].imag+=Symbol[Length-j*8+i].imag*Taps_power[j];
		}
	}

	for(i=16;i<24;i++)
	{
		for(j=3;j<5;j++)
		{
			ISI_temp[i].real+=Symbol[Length-j*8+i].real*Taps_power[j];
			ISI_temp[i].imag+=Symbol[Length-j*8+i].imag*Taps_power[j];
		}
	}

	for(i=24;i<32;i++)
	{
		for(j=4;j<5;j++)
		{
			ISI_temp[i].real+=Symbol[Length-j*8+i].real*Taps_power[j];
			ISI_temp[i].imag+=Symbol[Length-j*8+i].imag*Taps_power[j];
		}
	}


	/*calculate the output of the symbol corresponding to 5-path*/
	for(i=(Length-1);i>=32;i--)
	{
		tmp.real=Symbol[i-8].real*Taps_power[1]+Symbol[i-16].real*Taps_power[2]+Symbol[i-24].real*Taps_power[3]+Symbol[i-32].real*Taps_power[4];
		tmp.imag=Symbol[i-8].imag*Taps_power[1]+Symbol[i-16].imag*Taps_power[2]+Symbol[i-24].imag*Taps_power[3]+Symbol[i-32].imag*Taps_power[4];
		Symbol[i].real=Symbol[i].real*Taps_power[0]+tmp.real;
		Symbol[i].imag=Symbol[i].imag*Taps_power[0]+tmp.imag;
		tmp.real=0;
		tmp.imag=0;
	}

	/*From 0 to 31 samples,the ISI is generated from two parts: 
	/*the samples of the temporary symbol and the previous one*/
	for(i=31;i>=24;i--)
	{
		tmp.real=Symbol[i-8].real*Taps_power[1]+Symbol[i-16].real*Taps_power[2]+Symbol[i-24].real*Taps_power[3]+ISI[i].real;
		tmp.imag=Symbol[i-8].imag*Taps_power[1]+Symbol[i-16].imag*Taps_power[2]+Symbol[i-24].imag*Taps_power[3]+ISI[i].imag;
		Symbol[i].real=Symbol[i].real*Taps_power[0]+tmp.real;
		Symbol[i].imag=Symbol[i].imag*Taps_power[0]+tmp.imag;
		tmp.real=0;
		tmp.imag=0;
	}

	for(i=23;i>=16;i--)
	{
		tmp.real=Symbol[i-8].real*Taps_power[1]+Symbol[i-16].real*Taps_power[2]+ISI[i].real;
		tmp.imag=Symbol[i-8].imag*Taps_power[1]+Symbol[i-16].imag*Taps_power[2]+ISI[i].imag;
		Symbol[i].real=Symbol[i].real*Taps_power[0]+tmp.real;
		Symbol[i].imag=Symbol[i].imag*Taps_power[0]+tmp.imag;
		tmp.real=0;
		tmp.imag=0;
	}

	for(i=15;i>=8;i--)
	{
		tmp.real=Symbol[i-8].real*Taps_power[1]+ISI[i].real;
		tmp.imag=Symbol[i-8].imag*Taps_power[1]+ISI[i].imag;
		Symbol[i].real=Symbol[i].real*Taps_power[0]+tmp.real;
		Symbol[i].imag=Symbol[i].imag*Taps_power[0]+tmp.imag;
		tmp.real=0;
		tmp.imag=0;
	}

	for(i=7;i>=0;i--)
	{
		tmp.real=ISI[i].real;
		tmp.imag=ISI[i].imag;
		Symbol[i].real=Symbol[i].real*Taps_power[0]+tmp.real;
		Symbol[i].imag=Symbol[i].imag*Taps_power[0]+tmp.imag;
		tmp.real=0;
		tmp.imag=0;
	}
    for(i = 0; i < MaxDelay; i++)
	{
		ISI[i]=ISI_temp[i];
	}
}


